<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib uri="/WEB-INF/tlds/struts-html.tld"  prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/custom-html.tld" prefix="chtml" %>


<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collection" %>
<%@ page import="com.erp.app.common.ProjectConstants" %>
<%@ page import="com.erp.app.util.web.WebUtil" %>
<%@ page import="com.erp.framework.util.CommonUtil" %>
<%@ page import="com.erp.app.report.genericInquiry.inquiryMaster.beans.*" %>
<%@ page import="com.erp.app.report.genericInquiry.inquiryCriteria.beans.vo.*" %>
<%@ page import="com.erp.app.report.genericInquiry.GenericInquiryUtil" %>
<%@ page import="com.erp.app.report.genericInquiry.*"%>
<%@ page import="com.erp.app.report.genericInquiry.GenericInquiryConstants" %>


<jsp:useBean id="genericInquirySearchForm" scope="session" type="GenericInquirySearchForm" />
<%	
	GenericInquiryMasterVO masterVO = genericInquirySearchForm.getGenericInquiryMasterVO();

	Collection list = null;
	
	if (masterVO != null) {
		list = masterVO.getLocalChilds(InquiryCriteriaVO.class);
		pageContext.setAttribute("list", list); 
	}
		
	StringBuffer query = null;
	String label = null;
	String srchFrom = null;
	String srchTo = null;
	String srchFromLabel = null;		
%>

<logic:present name="list">
	<table class="table_common" id="srch_crit_input_tbl" border="0" cellpadding="0" cellspacing="1" style="display:<%=genericInquirySearchForm.isSearched() && !genericInquirySearchForm.isFileDownload() ? "none" : "block"%>">
		<tr>  
			<td>
				<table class="table_crit">
					<tr class="col_label"> 
						<td class="col_label">&nbsp;</td>
						<td class="col_label_fr_to">From</td>
						<td class="col_label_fr_to">To</td>
					</tr>
					<logic:iterate id="criteria" name="list" indexId="idx" type="InquiryCriteriaVO">
					<%	
					query = new StringBuffer();
					label = CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY) 
					|| CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY_WITH_BLANK) ? 
					"*" 
					: 
					"";

					boolean isMandatory = CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY) 
					|| CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY_WITH_BLANK);

					label += CommonUtil.nullToString(criteria.getFieldName());
					if (CommonUtil.nullToString(criteria.getFieldNature()).equalsIgnoreCase(GenericInquiryConstants.DATA_TYPE_DATE))
						label += ProjectConstants.WEB_DATE_FORMAT_LABEL;
					
					srchFrom = genericInquirySearchForm.getSrchFrom() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchFrom()[idx.intValue()]);
					srchTo = genericInquirySearchForm.getSrchTo() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchTo()[idx.intValue()]);
					srchFromLabel = genericInquirySearchForm.getSrchFromLabel() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchFromLabel()[idx.intValue()]);
					%>
						<tr class="col_label">
							<td class="col_label" nowrap>
								<% if (isMandatory) { %>
									<span class="mandatory_label">
								<% } %>
								<%=label%>:
								<% if (isMandatory) { %>
									</span>
								<% } %>
							</td>
							<!-- one text box -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT%>">
								<td class="col_value" colspan="2">
									<html:text 
											property="srchFrom" 
											maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
											size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>"
											styleClass="textbox1"
											value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- from and to text box -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO%>">
								<td class="col_value">
									<input type="text" name="srchFrom" 
										maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
										size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>"
										class="textbox1"
										value="<%=srchFrom%>"
										autofill="false">
								</td>
								<td class="col_value">
									<html:text 
										property="srchTo" 
										maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
										size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>"
										styleClass="textbox1"
										value="<%=srchTo%>"/>
									<html:hidden property="srchFromLabel" value=""/>
								</td>
							</logic:equal>
							<!-- text box  -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO_OC %>">
								<td class="col_value">
									<input type="text" name="srchFrom" 
										maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
										size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>"
										class="textbox1"
										value="<%=srchFrom%>"
										autofill="true">
								</td>
								<td class="col_value">
									<html:text 
										property="srchTo" 
										maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
										size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>"
										styleClass="textbox1"
										value="<%=srchTo%>"/>
									<html:hidden property="srchFromLabel" value=""/>
								</td>
							</logic:equal>		
							<!-- drop down -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_DROP_DOWN %>">
								<td class="col_value" colspan="2">
									<select name="srchFrom" class="selectbox1">
										<% if (!CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY)) { %>
										<option value=""></option>
										<% } %>
										<chtml:options table="<%=criteria.getDdFrom()%>"
												valuecolumn="<%=criteria.getDdValue()%>"
												textcolumn="<%=criteria.getDdSelect()%>"
												where="<%=GenericInquiryUtil.parseQueryString(genericInquirySearchForm, session, criteria.getDdWhere(), idx.intValue())%>"
												order="<%=criteria.getDdOrder()%>"
												selected="<%=srchFrom%>"/>
									</select>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- drop down -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_DROP_DOWN_OC%>">
								<td class="col_value" colspan="2">
									<select name="srchFrom" onchange="doSubmit('dropdownOC')" class="selectbox1">
										<% if (!CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY)) { %>
										<option value=""></option>
										<% } %>
										<chtml:options table="<%=criteria.getDdFrom()%>"
												valuecolumn="<%=criteria.getDdValue()%>"
												textcolumn="<%=criteria.getDdSelect()%>"
												where="<%=GenericInquiryUtil.parseQueryString(genericInquirySearchForm, session, criteria.getDdWhere(), idx.intValue())%>"
												order="<%=criteria.getDdOrder()%>"
												selected="<%=srchFrom%>"/>
									</select>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>		
							<!-- multiple drop down -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIPLE_DROP_DOWN%>">
								<td  class="col_value" colspan="2">
									<select class="selectbox1" id="<%=GenericInquiryConstants.ID_MULTIPLE_DROP_DOWN%>" name="<%=idx.intValue()%>" multiple size="<%=CommonUtil.nullToString(criteria.getMddHeight())%>">
										<chtml:options table="<%=criteria.getDdFrom()%>"
												valuecolumn="<%=criteria.getDdValue()%>"
												textcolumn="<%=criteria.getDdSelect()%>"
												where="<%=GenericInquiryUtil.parseQueryString(genericInquirySearchForm, session, criteria.getDdWhere(), idx.intValue())%>"
												order="<%=criteria.getDdOrder()%>"
												selected="<%=srchFrom%>"/>
									</select>
									<html:hidden property="srchFrom" value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- pop up select -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT%>">
								<td class="col_value" colspan="2">
									<span class="normal_text"><%=srchFromLabel%></span>
									<input type="button" class="button1" name="Select <%=criteria.getFieldName()%>" value="Select" onclick="javascript:doSubmit('popupSearch', '<%=idx.intValue()%>')">
									<input type="button" class="button1" name="Clear <%=criteria.getFieldName()%>" value="Clear" onclick="javascript:doSubmit('clearPopup', <%=idx.intValue()%>)">
									<html:hidden property="srchFrom" value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- pop up select multiple -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT_MULTIPLE%>">
								<td class="col_value" colspan="2">
									<% if (!CommonUtil.isEmpty(srchFromLabel)) { %>
									<div style="width:250px;overflow:auto;float:left"><span class="normal_text"><%=srchFromLabel%></span></div>
									<% } %>
									<input type="button" class="button1" name="Select <%=criteria.getFieldName()%>" value="Select" onclick="javascript:doSubmit('popupSearch', '<%=idx.intValue()%>')">
									<input type="button" class="button1" name="Clear <%=criteria.getFieldName()%>" value="Clear" onclick="javascript:doSubmit('clearPopup', <%=idx.intValue()%>)">
									<html:hidden property="srchFrom" value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- pop up select text -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT_TEXT%>">
								<td class="col_value" colspan="2">
									<html:text 
										property="srchFrom" 
										maxlength="<%=CommonUtil.nullToString(criteria.getMaxLength())%>" 
										styleClass="text1"
										size="<%=CommonUtil.nullToString(criteria.getFieldSize())%>" 
										value="<%=srchFrom%>"/>

									<input type="button" class="button1" name="Select <%=criteria.getFieldName()%>" value="Search" onclick="javascript:doSubmit('popupSearch', <%=idx.intValue()%>)">
					
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- multibox -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIBOX%>">
								<td class="col_value" colspan="2">
									<%
									String cbName = GenericInquiryConstants.ID_MULTIPLE_CHECKBOX + idx;
									String onClick = "setMultiBoxLabel('"+idx+"','"+cbName+"')";
									%>
									<chtml:multiBox
											name="<%=cbName%>"
											table="<%=criteria.getDdFrom()%>"
											valuecolumn="<%=criteria.getDdValue()%>"
											textcolumn="<%=criteria.getDdSelect()%>"
											where="<%=GenericInquiryUtil.parseQueryString(genericInquirySearchForm, session, criteria.getDdWhere(), idx.intValue())%>"
											order="<%=criteria.getDdOrder()%>"
											onClick="<%=onClick%>"
											checked="<%=request.getParameterValues(cbName)%>" />
									<input type="hidden" name="cbIdx" value="<%=idx%>">
									<html:hidden property="srchFrom" value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
							<!-- multibox horizontal -->
							<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIBOX_HORIZONTAL%>">
								<td class="col_value" colspan="2">
									<%
									String cbName = GenericInquiryConstants.ID_MULTIPLE_CHECKBOX + idx;
									String onClick = "setMultiBoxLabel('"+idx+"','"+cbName+"')";
									%>
									<chtml:multiBox
											name="<%=cbName%>"
											table="<%=criteria.getDdFrom()%>"
											valuecolumn="<%=criteria.getDdValue()%>"
											textcolumn="<%=criteria.getDdSelect()%>"
											where="<%=GenericInquiryUtil.parseQueryString(genericInquirySearchForm, session, criteria.getDdWhere(), idx.intValue())%>"
											order="<%=criteria.getDdOrder()%>"
											onClick="<%=onClick%>"
											checked="<%=request.getParameterValues(cbName)%>" />

				<%--							displayStyle="Horizontal" />--%>
									<input type="hidden" name="cbIdx" value="<%=idx%>">
									<html:hidden property="srchFrom" value="<%=srchFrom%>"/>
									<html:hidden property="srchTo" value=""/>
									<html:hidden property="srchFromLabel" value="<%=srchFromLabel%>"/>
								</td>
							</logic:equal>
						</tr>
					</logic:iterate>
				</table>	
			</td>
		</tr>
	</table>
	<logic:equal name="genericInquirySearchForm" property="searched" value="true">
		<logic:equal name="genericInquirySearchForm" property="fileDownload" value="false">
			<script type="text/javascript">document.getElementById("srch_crit_input_tbl").style.display = "none";</script>
			<table class="table_common" id="srch_crit_tbl" border="0" cellpadding="0" cellspacing="1">
				<tr>  
					<td>
						<table class="table_crit">
							<tr class="col_label"> 
								<td class="col_label">&nbsp;</td>
								<td class="col_label_fr_to" style="width:200px">From</td>
								<td class="col_label_fr_to" style="width:200px">To</td>
							</tr>
							<logic:iterate id="criteria" name="list" indexId="idx" type="InquiryCriteriaVO">
							<%	
								query = new StringBuffer();
								label = CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY) 
										|| CommonUtil.nullToString(criteria.getMandatoryFlag()).equalsIgnoreCase(GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY_WITH_BLANK) ? 
											"*" 
											: 
											"";
								label += CommonUtil.nullToString(criteria.getFieldName());
								if (CommonUtil.nullToString(criteria.getFieldNature()).equalsIgnoreCase(GenericInquiryConstants.DATA_TYPE_DATE))
									label += ProjectConstants.WEB_DATE_FORMAT_LABEL;
								
								srchFrom = genericInquirySearchForm.getSrchFrom() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchFrom()[idx.intValue()]);
								srchTo = genericInquirySearchForm.getSrchTo() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchTo()[idx.intValue()]);
								srchFromLabel = genericInquirySearchForm.getSrchFromLabel() == null ? "" : CommonUtil.nullToString(genericInquirySearchForm.getSrchFromLabel()[idx.intValue()]);
								
								if (!CommonUtil.isEmpty(srchFrom) || !CommonUtil.isEmpty(srchTo)) {
							%>
							<tr class="col_label">
								<td class="col_label" nowrap><%=label%>:</td>
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFrom%></span>
									</td>
								</logic:equal>
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO%>">
									<td class="col_value">
										<span class="normal_text"><%=srchFrom%></span>
									</td>
									<td class="col_value">
										<span class="normal_text"><%=srchTo%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO_OC%>">
									<td class="col_value">
										<span class="normal_text"><%=srchFrom%></span>
									</td>
									<td class="col_value">
										<span class="normal_text"><%=srchTo%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_DROP_DOWN%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_DROP_DOWN_OC%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIPLE_DROP_DOWN%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>		
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT_MULTIPLE%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_POPUP_SELECT_TEXT%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFrom%></span>
									</td>
								</logic:equal>
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIBOX%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>
								<logic:equal name="criteria" property="fieldType" value="<%=GenericInquiryConstants.FIELD_TYPE_MULTIBOX_HORIZONTAL%>">
									<td colspan="2" class="col_value">
										<span class="normal_text"><%=srchFromLabel%></span>
									</td>
								</logic:equal>
							</tr>
							<%	} %>
							</logic:iterate>
						</table>	
					</td>
				</tr>
				<tr>
					<td>
						<table>
							<tr>
								<td><input type="button" value="Show all criteria" onclick="showCrit()" class="button1"></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</logic:equal> <!-- fileDownload = false -->
	</logic:equal> <!-- searched = true -->
</logic:present>